﻿<div class="form-horizontal" ms-controller="TKST">
    <div style="margin: 0 80px;">
        <div>
            <ul class="clearfix" style="display: block;padding:0">
                <li class="add-item add-widthall">
                    <label class="add-item-label">知识点</label>
                    <div class="add-ic">
                        <input type="text" class="form-control" ms-duplex="modelData.KnowLedge" id="ddlknow" placeholder="请输入知识点" />
                    </div>
                </li>
                <li class="add-item add-widthall">
                    <label class="add-item-label"><i>*</i>试题类型</label>
                    <div class="add-ic">
                        <select class="form-control szhl_require" ms-duplex="modelData.Type" ms-change="ChangeSTType()">
                            <option ms-attr-selected="modelData.Type=='单选题'?'selected':''" value="单选题">单选题</option>
                            <option ms-attr-selected="modelData.Type=='多选题'?'selected':''" value="多选题">多选题</option>
                            <option ms-attr-selected="modelData.Type=='判断题'?'selected':''" value="判断题">判断题</option>
                            <option ms-attr-selected="modelData.Type=='填空题'?'selected':''" value="填空题">填空题</option>
                            <option ms-attr-selected="modelData.Type=='计算题'?'selected':''" value="计算题">计算题</option>
                            <option ms-attr-selected="modelData.Type=='绘图题'?'selected':''" value="绘图题">绘图题</option>
                            <option ms-attr-selected="modelData.Type=='论述题'?'selected':''" value="论述题">论述题</option>
                        </select>
                    </div>
                </li>
                <li class="add-item add-widthall">
                    <label class="add-item-label"><i>*</i>难易程度</label>
                    <div class="add-ic">
                        <select class="form-control szhl_require" ms-duplex="modelData.Level">
                            <option ms-attr-selected="modelData.Level==1?'selected':''" value="1">容易</option>
                            <option ms-attr-selected="modelData.Level==2?'selected':''" value="2">较易</option>
                            <option ms-attr-selected="modelData.Level==3?'selected':''" value="3">一般</option>
                            <option ms-attr-selected="modelData.Level==4?'selected':''" value="4">较难</option>
                            <option ms-attr-selected="modelData.Level==5?'selected':''" value="5">难</option>
                        </select>
                    </div>
                </li>
                <li class="add-item add-widthall">
                    <label class="add-item-label"><i>*</i>题文</label>
                    <div class="add-ic">
                        <textarea ms-duplex="modelData.QContent" id="tw" rows="4" class="form-control szhl_UEEDIT"></textarea>
                    </div>
                </li>
                <li class="add-item add-widthall" ms-if="modelData.Type!='单选题'&&modelData.Type!='多选题'&&modelData.Type!='判断题'">
                    <label class="add-item-label"><i>*</i>答案</label>
                    <div class="add-ic">
                        <textarea ms-duplex="modelData.QAnswer" rows="4" id="da" class="szhl_UEEDIT"></textarea>
                    </div>
                </li>
                <li class="add-item add-widthall" ms-visible="modelData.Type=='单选题'||modelData.Type=='多选题'||modelData.Type=='判断题'">
                    <label class="add-item-label"><i>*</i>选项详细</label>
                    <div class="detail-item" style="padding-left:0px;">
                        <table class="table table-hover table-bordered" id="tab">
                            <thead style="background:#f7f7f7;">
                                <tr>
                                    <th width="100px" class="tc">是否为答案</th>
                                    <th width="100px" class="tc">选项名称</th>
                                    <th>选项内容</th>
                                    <th width="100px">操作</th>

                                </tr>
                            </thead>
                            <tbody>

                                <tr ms-repeat-item="optionData">
                                    <td class="tc">
                                        <div class="icheckbox_square-blue checked">
                                            <span class="iconfont icon-check ft12"></span>
                                        </div>
                                    </td>
                                    <td class="tc">{{item.ItemName}}</td>
                                    <td>
                                        <input type="text" ms-if="modelData.Type!='判断题'&&item.ItemDesc.indexOf('img')<0" ms-blur="onBlur()" class="form-control" ms-duplex="item.ItemDesc" />
                                        <label ms-if="modelData.Type=='判断题'||item.ItemDesc.indexOf('img')>-1">{{item.ItemDesc|html}}</label>
                                    </td>
                                    <td><input type='button' ms-if="modelData.Type!='判断题'" ms-click="DELOPTION(item)" class='btn btn-sm btn-warning' value='删除' /></td>

                                </tr>

                            </tbody>
                            <tfoot ms-visible="modelData.Type!='判断题'">
                                <tr>
                                    <td colspan="3">
                                        <input type="text" id="content" ms-duplex="optionContent" class="form-control" placeholder="请输入选项内容" />
                                    </td>
                                    <td style="width: 80px;">
                                        <input type="button" value="添加" class="btn btn-sm btn-info" ms-click="AddOption()" />
                                    </td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                    <!--<div class="add-ic">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>是否为答案</th>
                                    <th>选项名称</th>
                                    <th>选项内容</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr ms-repeat-item="optionData">
                                    <td>
                                        <input type="checkbox" ms-click="OnCheck(item,this)" ms-attr-checked="Answer.indexOf(item.ItemName)>-1" />
                                    </td>
                                    <td>{{item.ItemName}}</td>
                                    <td>
                                        <input type="text" ms-if="modelData.Type!='判断题'&&item.ItemDesc.indexOf('img')<0" ms-blur="onBlur()" class="form-control" ms-duplex="item.ItemDesc" />
                                        <label ms-if="modelData.Type=='判断题'||item.ItemDesc.indexOf('img')>-1">{{item.ItemDesc|html}}</label>
                                    </td>
                                    <td>
                                        <input type='button' ms-if="modelData.Type!='判断题'" ms-click="DELOPTION(item)" class='btn btn-sm btn-warning' value='删除' />
                                    </td>
                                </tr>
                            </tbody>
                            <tfoot ms-visible="modelData.Type!='判断题'">
                                <tr>
                                    <td colspan="3">
                                        <input type="text" id="content" ms-duplex="optionContent" class="form-control" />
                                    </td>
                                    <td style="width: 80px;">
                                        <input type="button" value="添加" class="btn btn-sm btn-info" ms-click="AddOption()" />
                                    </td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>-->
                </li>
            </ul>
        </div>

    </div>
</div>

<script>
    var tempmodel = avalon.define({
        $id: "TKST",
        name: "试题管理",
        iswf: true,//是否属于流程表单
        ColumnData: [],
        TypeData: [],
        optionNames: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'],
        optionData: [],
        Answer: "",
        tkid: ComFunJS.getQueryString("tkid", 0),
        inittemp: function (strId) {
            $.getJSON('/API/VIEWAPI.ashx?ACTION=KSGL_GETKNOWLEDGE', { P1: tempmodel.tkid }, function (result) {
                if (result.ErrorMsg == "") {
                    var KnowLedge = [];
                    result.Result.forEach(function (item) {
                        if (item.KnowLedge) {
                            KnowLedge.push(item.KnowLedge);
                        }
                    })
                    $('#ddlknow').typeahead({
                        source: function (query, process) {
                            return KnowLedge;
                        }
                    })

                }
            })
            if (strId) {
                //编辑加载数据
                $.getJSON('/API/VIEWAPI.ashx?Action=KSGL_GETTKSTMODEL', { P1: strId }, function (resultData) {
                    if (resultData.ErrorMsg == "") {
                        tempmodel.modelData = resultData.Result;
                        if (resultData.Result1.length > 0) {
                            tempmodel.optionData = resultData.Result1;
                        } else {
                            tempmodel.ChangeSTType();
                        }
                        setTimeout(" ComFunJS.initForm()", 500);
                    }
                })
            } else {
                tempmodel.ChangeSTType();
                ComFunJS.initForm();
            }

        },//初始化
        modelData: { "KnowLedge": "", "TKID": ComFunJS.getQueryString("tkid", 0), "QContent": "", "QAnswer": "", "Type": "单选题", "Level": 3 },
        SaveData: function (callback) {
            if (!ComFunJS.convstr(tempmodel.modelData.QContent)) {
                ComFunJS.winwarning("题文不能为空");
                $(dom).attr("disabled", true).find(".fa").hide();//加上转圈样式
                return;
            }
            if (tempmodel.Answer) {
                tempmodel.modelData.QAnswer = tempmodel.Answer;
            }
            var xzArray = ["单选题", "多选题", "判断题"];
            if (xzArray.indexOf(tempmodel.modelData.Type) > -1 && tempmodel.optionData.size() == 0) {

                top.ComFunJS.winwarning("请给题目添加选项");
                return;

            }
            $.post("/API/VIEWAPI.ashx?ACTION=KSGL_ADDTKST", { P1: JSON.stringify(tempmodel.modelData.$model), P2: JSON.stringify(tempmodel.optionData.$model) }, function (result) {
                return callback.call(this, $.parseJSON(result));
            });
        }, optionContent: "", AddOption: function () {
            var option = { "ItemName": "", "ItemDesc": "" };
            option.ItemName = tempmodel.optionNames[tempmodel.optionData.length];
            option.ItemDesc = tempmodel.optionContent;
            if (!option.ItemDesc) {
                top.ComFunJS.winwarning("选项内容不能为空");
                return;
            }
            var flag = true;
            if (tempmodel.optionData.length > 0) {
                tempmodel.optionData.forEach(function (val) {
                    if (val.ItemName == option.ItemName) {
                        flag = false;
                        top.ComFunJS.winwarning("已有此选项，请修改选项名称");
                        return;
                    }
                })
            }
            if (flag) {
                tempmodel.optionData.push(option);
                tempmodel.optionContent = "";
            }
        }, DELOPTION: function (item) {
            tempmodel.optionData.remove(item);
            tempmodel.Answer = tempmodel.Answer.replace(item.ItemName, "");
            $(tempmodel.optionData).each(function (i, val) {
                val.ItemName = optionNames[i];
            })
        }, ChangeSTType: function () {
            tempmodel.Answer = "";
            tempmodel.optionData.clear();
            if (tempmodel.optionData.size() == 0) {
                if (tempmodel.modelData.Type == "判断题") {
                    tempmodel.optionData.push({ "ItemName": "A", "ItemDesc": "×" });
                    tempmodel.optionData.push({ "ItemName": "B", "ItemDesc": "√" });
                }
                if (tempmodel.modelData.Type == "单选题" || tempmodel.modelData.Type == "多选题") {
                    tempmodel.optionData.push({ "ItemName": "A", "ItemDesc": "" });
                    tempmodel.optionData.push({ "ItemName": "B", "ItemDesc": "" });
                    tempmodel.optionData.push({ "ItemName": "C", "ItemDesc": "" });
                    tempmodel.optionData.push({ "ItemName": "D", "ItemDesc": "" });
                    if (tempmodel.modelData.Type == "多选题") {

                        tempmodel.optionData.push({ "ItemName": "E", "ItemDesc": "" });
                        tempmodel.optionData.push({ "ItemName": "F", "ItemDesc": "" });
                    }
                }

            }
        }, OnCheck: function (item, dom) {
            if ($(dom).prop("checked")) {
                if (tempmodel.Answer) {
                    if (tempmodel.modelData.Type == "多选题") {
                        tempmodel.Answer = tempmodel.Answer + item.ItemName;
                    } else {
                        $("input[type=checkbox]:checked").prop("checked", false);
                        $(dom).prop("checked", true);
                    }
                } else {
                    tempmodel.Answer = item.ItemName;
                }
            } else {
                tempmodel.Answer = tempmodel.Answer.replace(item.ItemName, "");
            }
        }

    });

    tempmodel.$watch("modelData.*", function (a, b) {
        if (!tempmodel.modelData.ID) {
            localStorage.setItem(pmodel.FormCode, JSON.stringify(tempmodel.modelData.$model));
        }
    })//@ sourceURL=TKST.js;

</script>
